package org.sjm.samples.dao;


import java.util.List;

import org.hibernate.transform.Transformers;
import org.sjm.samples.model.ExamStudentVO;
import org.sjm.samples.model.User;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class UserDAOImpl extends BaseHibernateDAO<User> implements IUserDAO {

	@Override
	public List<User> FindAllUsers() {
		return this.find("FROM User");
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ExamStudentVO> FindUserByStudent() {
		String hql = " SELECT u.userId AS id, u.userName AS name FROM User AS u, ExamUserRole AS e WHERE u.userId=e.user.userId AND e.examRole.roleId=25 ";
		return this.createQuery(hql).setResultTransformer(Transformers.aliasToBean(ExamStudentVO.class)).list();
	}
	
	@Override
	public User getUserById(String userId) {
		return this.get(userId);
	}

	@Override
	public Integer createUser(User user) throws Exception {
		this.save(user);
		return 1;
	}

	@Override
	public Integer editUser(User user) throws Exception {
		this.update(user);
		return 1;
	}

	@Override
	public Integer deleteUser(String userId) throws Exception {
		User exsitsUser = this.get(userId);
		if(exsitsUser != null){
			this.delete(exsitsUser);
		}
		return 1;
	}

	@Override
	public void saveUser(List<User> userList) {
		this.save(userList);
	}
	
	/*
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	private static final String FIND_ALL_USER = " SELECT user_id,user_name,gender,birthday,tel,address,login_fail_count FROM exam_user ";
	
	private static final String FIND_BY_ID = " SELECT * FROM exam_user WHERE user_id=? ";
	
	private static final String FIND_CREATE_USER = " INSERT INTO exam_user VALUES (?,?,?,?,?,?,?,?,?,?) ";
	
	private static final String FIND_EDIT_USER = " UPDATE exam_user SET user_id=?,user_name=?,tel=?,address=?,birthday=?,gender=? WHERE user_id=? ";
	
	private static final String FIND_DELETE_USER = " DELETE FROM exam_user WHERE user_id=? ";


	public static final class UserMapper implements RowMapper<User>{
		public User mapRow(ResultSet rs, int rowNum) throws SQLException {
			User user = new User();
			user.setUserId(rs.getString("user_id"));
			user.setUserName(rs.getString("user_name"));
			user.setGender(String.valueOf(rs.getInt("gender")));
			user.setBirthday(rs.getString("birthday"));
			user.setTel(rs.getString("tel"));
			user.setAddress(rs.getString("address"));
			user.setLoginFailCount(rs.getInt("login_fail_count"));
			return user;
		}
	}
	
	@Override
	public List<User> findAllUsers() {
		List<User> users = jdbcTemplate.query(FIND_ALL_USER, new UserMapper());
		return users;
	}

	@Override
	public Integer createUser(User user) throws Exception {
		if(user != null && getUserById(user.getUserId()) == null){
			return jdbcTemplate.update(FIND_CREATE_USER, new Object[]{user.getUserId(), user.getUserName(), user.getPassword(), user.getTel(), user.getEmail(), user.getAddress(), user.getBirthday(), user.getGender(), user.getLastLockTime(), user.getLoginFailCount()});
		}else{
			throw new Exception("Create User Fail!");
		}
	}

	@Override
	public Integer deleteUser(String userId) throws Exception {
		if(getUserById(userId) != null){
			return jdbcTemplate.update(FIND_DELETE_USER, new Object[]{userId});
		}else{
			throw new Exception("Delete User Fail!");
		}
	}

	@Override
	public User getUserById(String userId) {
		List<User> users = jdbcTemplate.query(FIND_BY_ID, new Object[]{userId}, new UserMapper());
		if(users.size() > 0){
			return users.get(0);
		}else{
			return null;
		}
	}

	@Override
	public Integer editUser(User user) throws Exception {
		if(user != null && getUserById(user.getUserId()) != null){
			return jdbcTemplate.update(FIND_EDIT_USER, new Object[]{user.getUserId(), user.getUserName(), user.getTel(), user.getAddress(), user.getBirthday(), user.getGender(), user.getUserId()});
		}else{
			throw new Exception("Update User Fail!");
		}
	}
	 */
	
}
